<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            height: 100vh;
            background-color: #f0f0f0;
        }
        ul {
            list-style: none;
            width: 120px;
            padding: 20px;
            border: 1px solid #999;
            border-radius: 6px;
            position: absolute;
        }
    </style>
</head>
<body>
    <p>自定义菜单栏</p>
    <ul class="ul" hidden>
        <li>重新加载</li>
        <li>显示源代码</li>
        <li>检查元素</li>
        <li>复制</li>
        <li>粘贴</li>
        <li>剪切</li>
    </ul>
    <script>
        var ul = document.querySelector('.ul')
        // 1. 阻止默认事件
        document.body.oncontextmenu = function (e) {
            // 4.跟随鼠标
            ul.style.top = e.clientY + 'px'
            ul.style.left = e.clientX + 'px'
            e.preventDefault();
            e.returnValue = false;
            // 2. 显示自定义菜单
            ul.style.display = 'block';
        }

        // 3. 点击空白隐藏菜单
        document.body.onclick = function(e) {
            e.stopPropagation(); // 普通浏览器
            event.cancelBubble=true; // ie浏览器
            ul.style.display = 'none';
        }
    </script>
</body>
</html>